// 基于 SnowLang Project (https://gitee.com/jcnc-org/snow) 的衍生作品
// 原始版权归属: Copyright © 2025 Ke Xu (Luke)
// 本实现版权归属: Copyright © 2025 Sany
// 遵循 Apache License 2.0 授权
package control

import (
	"errors"
	"strconv"

	"gitee.com/xntk2381/cc/vm/module"
	"gitee.com/xntk2381/cc/vm/utils"
)

type FCGCommand struct{}

func (FCGCommand) Execute(parts []string, currentPC int, operandStack *module.OperandStack, localVariableStore *module.LocalVariableStore, callStack *module.CallStack) (int, error) {
	// Parse the target command address
	if len(parts) < 2 {
		return 0, errors.New("no target command address specified for FCG operation")
	}
	target, err := strconv.Atoi(parts[1])
	if err != nil {
		return 0, errors.New("invalid target command address specified for FCG operation")
	}

	valB, err := operandStack.Pop()
	if err != nil {
		return 0, err
	}
	b := valB.(float32)
	valA, err := operandStack.Pop()
	if err != nil {
		return 0, err
	}
	a := valA.(float32)

	// If the first operand is greater than the second, jump to the target command
	if a > b {
		utils.LogInfo("Jumping to command", parts[1])
		return target, nil
	}
	return currentPC + 1, nil
}
